/**
 * @file     ctxsw.s
 * @provides ctxsw
 *
 * COSC 3250 / COEN 4820 Assignment 4
 * Modified by
 * Andrew Kirkham
 * and
 * Chris Gozdziewski
 */
/* Embedded XINU, Copyright (C) 2010.  All rights reserved. */

#include <mips.h>
	
.text
	.align 4
	.globl	ctxsw
	.ent	ctxsw


/**
 * @fn void ctxsw(&oldstack, &newstack)
 *
 * Switch context (values in registers) to another process, saving the
 * current processes information. This function will not return as normally
 * thought as it will load in the stack pointer for a different process and
 * jump to that location and begin executing code.
 *
 * @param  &oldstack address of outgoing stack save area
 * @param  &newstack address of incoming stack save area
 * @return special case -- see above
 */
ctxsw:
	
	addi sp, sp, -CONTEXT 
	
	sw ra, CTX_JA(sp)
	sw ra, CTX_RA(sp)
	sw s0, CTX_S0(sp)
	sw s1, CTX_S1(sp)
	sw s2, CTX_S2(sp)
	sw s3, CTX_S3(sp)
	sw s4, CTX_S4(sp)
	sw s5, CTX_S5(sp)
	sw s6, CTX_S6(sp)
	sw s7, CTX_S7(sp)
	sw s8, CTX_S8(sp)
	sw s9, CTX_S9(sp)	

	sw sp, (a0)

	lw sp, (a1)
	lw a3, 76(sp)
	lw a2, 72(sp)
	lw a1, 68(sp)
	lw a0, 64(sp) 
	lw t0, CTX_JA(sp)
	lw ra, CTX_RA(sp)
	lw s0, CTX_S0(sp)
	lw s1, CTX_S1(sp)
	lw s2, CTX_S2(sp)
	lw s3, CTX_S3(sp)
	lw s4, CTX_S4(sp)
	lw s5, CTX_S5(sp)
	lw s6, CTX_S6(sp)
	lw s7, CTX_S7(sp)
	lw s8, CTX_S8(sp)
	lw s9, CTX_S9(sp)

	addi sp, sp, CONTEXT

	jr t0

	.end ctxsw
